/**
 * @author Administrator
 * 权限组树
 */
var  rolesgroupstore,rolesgroupwin,rolesgrouptempstore;
var  rolesgroupid="",rolesgroupname="",rolesgroupidtager="",rolesgroupnametager="";
function initrolesgrouptree(){
		 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";

	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/rolesgrouptree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,
	        height:420,
	        width:230,
	        anchor:'0% 100%'
	    });
	tree.setRootNode(root); 
	tree.on('dblclick',function(node){
			if(node.id!='0'&&rolesgroupstore.find('id',node.id) ==-1)
			{
				var orderNo = 1;
				if(rolesgroupstore.getCount()>0)
				{
					orderNo	= rolesgroupstore.getAt(rolesgroupstore.getCount() - 1).get('ordernumber')+1;
				}
				rolesgroupstore.add(new Ext.data.Record({
				ordernumber:orderNo,
				groupname:node.text,
				id:node.id
			}));	
		}		
	});
	/**
	 * 生成表格
	 */

	 var rolesgroupstoreurl = Ext.get('basePath').getValue() + 'tjsinfo/tjsoa/system/selectUserRolesGroup.do?userid='+Ext.get('userid').getValue();
	 rolesgroupstore = new Ext.data.JsonStore({
		url:rolesgroupstoreurl,
		fields:['ordernumber','groupname','id','operate']
	});
	rolesgrouptempstore = new Ext.data.JsonStore({
		url:rolesgroupstoreurl,
		fields:['ordernumber','groupname','id','operate']
	});
	rolesgroupstore.load();
	rolesgrouptempstore.load();
	var cm = new Ext.grid.ColumnModel([
		{header: "序号", width: 25, dataIndex: 'ordernumber',menuDisabled : true},
		{header: "部门名称", width: 75, dataIndex: 'groupname',menuDisabled : true},
		{header: "操作", width: 75, dataIndex: 'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href="+"javascript:rolesgroupremove('"+rec.data.id+"')>移除</a>";
		}},
		{header: "id", width: 75, dataIndex: 'id',menuDisabled : true,hidden:true}
	]);
	var grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		width:270,
		height:420,
		anchor:'100% 100%',
		store:rolesgroupstore,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
    rolesgroupwin = new Ext.Window({
		layout:'absolute',
		title:'权限选择',
		width:520,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				rolesgroupid='';
				rolesgroupname='';
				rolesgroupstore.each(function(record){
				rolesgroupid+=record.get('id')+",";
				rolesgroupname+=record.get('groupname')+",";
			
				});
				$("#"+rolesgroupidtager).attr('value',rolesgroupid);
				$("#"+rolesgroupnametager).attr('value',rolesgroupname);
				rolesgrouptempstore.removeAll();
				rolesgroupstore.each(function(record){
					rolesgrouptempstore.addSorted(record);	
				});
				rolesgroupwin.hide();
			}
		},{
			text:'取消',
			handler:function(){
				rolesgroupstore.removeAll();
				rolesgrouptempstore.each(function(record){
					rolesgroupstore.addSorted(record);	
				});
				rolesgroupwin.hide();		
			}
		}]
		
	})
//	win.show(Ext.getBody());
}
/**
 * 移除表格记录
 */
function rolesgroupremove(id){
	rolesgroupstore.remove(rolesgroupstore.getAt(rolesgroupstore.find('id',id)));
	
}
function rolesgroupwinshow(id,name){
	rolesgroupidtager = id;
	rolesgroupnametager = name;
	rolesgroupwin.show(Ext.getBody());
	rolesgroupwin.findByType('treepanel')[0].root.expand();  
}